package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsQuickExit;
import com.jy.wms.service.WmsQuickExitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: WmsQuickExitController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Sat Apr 28 09:08:37 CST 2018
 */
@RestController
@RequestMapping("/wmsquickexit")
@Api(value = "/wmsquickexit", description = "快速出库")
public class  WmsQuickExitController extends BaseController<WmsQuickExitService,WmsQuickExit> {

    @PostMapping(value = "/add")
    @ApiOperation(value = "快速出库 新增快速出库信息",response = JSONObject.class, notes = "新增快速出库信息")
    public @ResponseBody
    JSONObject add(HttpSession session, @RequestBody WmsQuickExit entity){
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setId(UUIDUtils.uuid());
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            baseService.baseInsert(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增快速出库信息成功","",  JSONObject.fromObject(entity).toString());
        }   catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增快速出库信息成功","",  JSONObject.fromObject(entity).toString());
        } catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增快速出库信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增快速出库信息失败", e.getMessage(),"");
        }
    }

    @GetMapping(value = "/getBh")
    @ApiOperation(value = "快速出库 获取快速出库发运编号",response = JSONObject.class, notes = "获取快速出库发运编号")
    public @ResponseBody
    JSONObject getBh(){
        JSONObject json = getJSONObject();
        try {
            json.put("fybh",baseService.getFybh());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @GetMapping(value = "/getXf/{id}/{ryid}")
    @ApiOperation(value = "快速出库 下发捡货任务",response = JSONObject.class, notes = "下发捡货任务")
    public @ResponseBody
    JSONObject getXf(@PathVariable String id,@PathVariable String ryid, HttpSession session){
        JSONObject json = getJSONObject();
        try {
            baseService.getXf(id,ryid,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"下发捡货任务成功","", "");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"下发捡货任务成功","", "");
        } catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"下发捡货任务失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"下发捡货任务失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "快速出库 分页查询",response = JSONObject.class, notes = "分页查询")
    @PostMapping(value = "/list")
    public @ResponseBody
    JSONObject list(@RequestBody WmsQuickExit entity, HttpSession session)  {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz = (List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            Page<WmsQuickExit> list = new Page<WmsQuickExit>();
            entity.setSshzid(hz.get(0).getHzid());
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            if(hz.size()>0){
                list = baseService.baseSelectPage(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
